/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import com.opensymphony.xwork2.ActionSupport;
import controller.CatererJpaController;
import controller.CustomerJpaController;
import controller.FavoriteJpaController;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.entity.Caterer;
import model.entity.Favorite;

/**
 *
 * @author lnngu_000
 */
@ManagedBean
public class CatererBean extends ActionSupport {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OnlineCateringPU");
    private Integer catererId;
    private String fullname;
    private String email;
    private String userId;
    private String password;
    private String phone;
    private Boolean isActive;
    private String descriptions;

    public Integer getCatererId() {
        return catererId;
    }

    public void setCatererId(Integer catererId) {
        this.catererId = catererId;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public String getDescriptions() {
        return descriptions;
    }

    public void setDescriptions(String descriptions) {
        this.descriptions = descriptions;
    }
    CatererJpaController catererControl = new CatererJpaController(emf);

    public String add() {
        try {
            Caterer caterer = new Caterer();
            caterer.setDescriptions(descriptions);
            caterer.setEmail(email);
            caterer.setFullname(fullname);
            caterer.setIsActive(true);
            caterer.setPassword(password);
            String uID;
            do {
                uID = randomCode();
                System.out.println(uID);
            } while (catererControl.findUId(uID) == 1);
            caterer.setUserId(uID);
            catererControl.create(caterer);
            emf.close();
            return "success";
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return "fail";
    }

    public String randomCode() {
        Random random = new Random();
        long startInteger = 100000;
        long endInteger = 999999;
        
        long range = endInteger - startInteger + 1;
        long fraction = (long) (range * random.nextDouble());
        long randomNumber = fraction + startInteger;
      
        String code = Long.toString(randomNumber);
        return code;
    }

    @Override
    public void validate() {
        if (this.getFullname().equals("")) {
            addActionError("Fullname is required.");
        } else {
        }
        if (this.getPassword().equals("")) {
            addActionError("Password is required.");
        } else {
        }
        if (this.getEmail().equals("")) {
            addActionError("Email is required.");
        } else {
            String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
            Pattern p = Pattern.compile(regex);
            Matcher matcher = p.matcher(this.getEmail().trim());
            if (matcher.find()) {
            } else {
                addActionError("Email address not validated.");
            }
        }
        if (this.getPhone().equals("")) {
            addActionError("Phone is required.");
        } else {
        }
    }
}
